using System.Windows.Forms;
using WindowsLive.Writer.Api;
using System.Text.RegularExpressions;
using System.Reflection;
using ScottIsAFool;

namespace CapitalizeTitle
{
    [WriterPlugin("20030fcb-a4c6-4f96-8d53-32127b0ec3bd", "Capitalize Title",
        PublisherUrl = "http://scottisafooldev.spaces.live.com",
        Description = "Capitalises the first letter of each word in your blog post. Can be automatic.",
        ImagePath = "writer.png",
        HasEditableOptions = true)]

    public class Plugin : PublishNotificationHook
    {
        Settings m_default;

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_default = new Settings(pluginOptions);
        }

        public override bool OnPrePublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            string origtitle = publishingContext.PostInfo.Title;
            string newtitle = Capitalise(origtitle);
            if (newtitle.Equals(origtitle))
            {
                return true;
            }
            else
            {
                if (m_default.Automatic)
                {
                    //return SetPostContents(publishingContext.PostInfo, newtitle);
                    return WriterUtils.ReplaceText(publishingContext.PostInfo, newtitle, PostItems.Title);
                }
                else
                {
                    DialogResult result = MessageBox.Show("Would you like to capitalise your post's Title?\n(Default can be set in Tools | Options | Plugins)", "Capitalize?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result == DialogResult.Yes)
                    {
                        //return SetPostContents(publishingContext.PostInfo, newtitle);
                        return WriterUtils.ReplaceText(publishingContext.PostInfo, newtitle, PostItems.Title);
                    }
                    else
                    {
                        return true;
                    }
                }
            }
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            frmOptions op = new frmOptions(m_default);
            op.ShowDialog();
        }       

        private string Capitalise(string title)
        {
            string result = "";
            string pattern = @"\w+|\W+";

            foreach (Match m in Regex.Matches(title, pattern))
            {
                // get the matched string
                string x = m.ToString();
                // if the first char is lower case
                if (char.IsLower(x[0]))
                    // capitalize it
                    x = char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
                // collect all text
                result += x;
            }
            return result;
        }
    }
}
